package work20241120;

import java.util.Random;
import java.util.Scanner;

/**
 * @author zdl
 * @date 2024/11/20
 */
public class Demo4 {
    public static void main(String[] args) {
        /**
         * 4.while循环练习：数字炸弹小游戏
         *  需求:程序自动生成一个1-100之间的随机数，使用程序实现猜出这个数字是多少？
         */
        int random = (int) (Math.random()*100)+1;
        System.out.println("随机数为:"+random);
        Scanner sc = new Scanner(System.in);

        int guess = 0;
        int count = 0;
        while (guess != random && count < 5){
            System.out.println("第"+(count+1)+"次猜数(1-100)：");
            guess = sc.nextInt();
            count++;
            if (guess > random){
                System.out.println("猜的有点儿大了");
            } else if (guess < random) {
                System.out.println("猜的有点儿小了");
            }else {
                System.out.println("恭喜你，用了"+count+"次机会猜对了");
            }
            if (count == 0){
                System.out.println(count+"次机会你都没有猜对");
            }

        }
//        while (count<0){
//            if (guess < random){
//                System.out.println("猜的有点小了");
//                count--;
//            } else if (guess > random) {
//                System.out.println("猜的有点儿大了");
//                count--;
//            }else {
//                System.out.println("恭喜你猜了"+count+"次，猜对了！");
//            }
//            if (count == 0){
//                System.out.println("5次全猜错了！！！");
//            }
//        }

    }
}
